package com.altio.swing.portlet;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.UIManager;

public class PortletPanelButton extends JButton {
  
    public static enum ActionOperation {
      MINIMIZE,
      CLOSE, RESTORE
    }
    
    /**
     * The UI class ID string.
     */
    public static final String uiClassID = "PortletPanelButtonUI";
    
    private ActionOperation actionOperation = ActionOperation.CLOSE;

    private PortletPanel portletPanel;
    
    public PortletPanelButton(final PortletPanel panel, final ActionOperation action) {
      super();
      this.actionOperation = action;
      this.portletPanel = panel;
      
      addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
          switch(actionOperation) {
            case CLOSE: {
              panel.close();
              break;
            }
            case MINIMIZE: {
              panel.setMinimized(true);
              setActionOperation(ActionOperation.RESTORE);
              break;
            }
            case RESTORE: {
              panel.setMinimized(false);
              setActionOperation(ActionOperation.MINIMIZE);
              break;
            }
          }
        }
      });
    }

    
    
    protected ActionOperation getActionOperation() {
      return actionOperation;
    }


    /*
     * (non-Javadoc)
     * 
     * @see javax.swing.JComponent#updateUI()
     */
    @Override
    public void updateUI() {
      if (UIManager.get(getUIClassID()) != null) {
        setUI(UIManager.getUI(this));
      } else {
        setUI(PortletPanelButtonUI.createUI(this));
      }
    }
    
    @Override
    public String getUIClassID() {
      return uiClassID;
    }



    public void setActionOperation(final ActionOperation actionOperation) {
      this.actionOperation = actionOperation;
      repaint();
    } 
  }